package org.szgb.console.ht.electricity.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtElectricityPrice extends BeanEntity<HtElectricityPrice> {
    @Serial
    private static final long serialVersionUID = 1L;
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM")
    private LocalDate month;
    private BigDecimal price;


    public HtElectricityPrice() {
    }

    public HtElectricityPrice(String id) {
        this.setId(id);
    }

    public HtElectricityPrice(HtElectricityPrice.HtElectricityPriceFindDto htElectricityPriceFindDto) {
        this.setId(htElectricityPriceFindDto.getId());
        if (htElectricityPriceFindDto.getMonth() != null) {
            this.setMonth(htElectricityPriceFindDto.getMonth().atDay(1));
        }
        this.setPageDto(htElectricityPriceFindDto.getPageDto());
    }

    public HtElectricityPrice(HtElectricityPrice.HtElectricityPriceSaveDto htElectricityPriceSaveDto) {
        this.setId(htElectricityPriceSaveDto.id);
        if (htElectricityPriceSaveDto.month != null) {
            this.setMonth(htElectricityPriceSaveDto.month.atDay(1));
        }
        this.setPrice(htElectricityPriceSaveDto.price);
    }

    public HtElectricityPrice.HtElectricityPriceView toHtElectricityPriceView() {
        return new HtElectricityPrice.HtElectricityPriceView(this);
    }

    @Data
    @Schema(title = "电价信息", description = "电价信息")
    public static class HtElectricityPriceView {
        @Schema(description = "ID")
        private String id;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "电价")
        private BigDecimal price;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtElectricityPriceView(HtElectricityPrice htElectricityPrice) {
            this.id = htElectricityPrice.getId();
            if (htElectricityPrice.getMonth() != null) {
                this.month = YearMonth.from(htElectricityPrice.getMonth());
            }
            this.price = htElectricityPrice.getPrice();
            this.createdAt = htElectricityPrice.getCreatedAt();
            this.updatedAt = htElectricityPrice.getUpdatedAt();
            if (htElectricityPrice.getCreatedBy() != null) {
                this.createdById = htElectricityPrice.getCreatedBy().getId();
                this.createdByName = htElectricityPrice.getCreatedBy().getName();
            }
            if (htElectricityPrice.getUpdatedBy() != null) {
                this.updatedById = htElectricityPrice.getUpdatedBy().getId();
                this.updatedByName = htElectricityPrice.getUpdatedBy().getName();
            }
        }

        public static List<HtElectricityPrice.HtElectricityPriceView> getViewListFromList(List<HtElectricityPrice> htElectricityPriceList) {
            return htElectricityPriceList.stream().map(HtElectricityPrice.HtElectricityPriceView::new).toList();
        }
    }

    @Data
    @Schema(title = "电价查询表单", description = "电价查询参数")
    public static class HtElectricityPriceFindDto {
        @Schema(description = "ID")
        private String id;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public HtElectricityPrice toHtElectricityPrice() {
            return new HtElectricityPrice(this);
        }
    }

    @Data
    @Schema(title = "电价保存表单", description = "电价保存参数")
    public static class HtElectricityPriceSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM")
        @Schema(description = "月份(格式yyyy-MM)")
        private YearMonth month;
        @Schema(description = "电价")
        private BigDecimal price;

        public HtElectricityPrice toHtElectricityPrice() {
            return new HtElectricityPrice(this);
        }
    }
}
